<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

//哈希加密
use Hash;
//数据库
use DB;
//数据验证
use App\Http\Requests\GoodsCreateRequest;
use App\Http\Requests\GoodsUpdateRequest;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
	/*
		显示商品添加
	*/
	public function getAdd()
	{
		//查询所有分类
		$cates = DB::table('cate')->get();
		//查询所有三级分类
		$data = [];
		foreach($cates as $k=>$v){
			if(count(explode(',',$v->path)) == 3){
				$data[] = $v;
			}
		}

		//查询所有店铺
		$market = DB::table('market')->get();

		//解析模板
		return view('admin.goods.add',['cates'=>$data,'market'=>$market]);
	}

	/*
		商品添加
	*/
	public function postInsert(GoodsCreateRequest $request)
	{
		//调用上传方法
		$upload = $this->upload($request,'goods_pic');
		//判断是否上传成功
		if($upload == 1){
			return back()->with('error','上传文件不合法');
		}
		//提取部分数据
		$data = $request->only(['cate_id','market_id','goods_price','goods_content','goods_name','rebate','goods_stock']);
		$data['goods_color'] = implode(',',$request->input('goods_color'));
		$data['goods_size'] = implode(',',$request->input('goods_size'));
		$data['goods_pic'] = $upload;

		// dd($data);
		$res = DB::table('goods')->insert($data);
		//判断
		if($res){
			return redirect('/admin/goods/index')->with('success','商品添加成功');
		}else{
			return back()->with('error','商品添加失败');
		}
	}

	/*
		商品列表
	*/
	public function getIndex(Request $request)
	{
		// $users = DB::table('users')
 	    //      ->join('contacts', 'users.id', '=', 'contacts.user_id')
        //      ->join('orders', 'users.id', '=', 'orders.user_id')
        //      ->select('users.*', 'contacts.phone', 'orders.price')
        //      ->get();
		$num = $request->input('num',10);
		//判断用户是否查询
		if(!empty($request->input('keywords'))){
			$goods = DB::table('goods')
				->join('cate','goods.cate_id','=','cate.id')
				->join('market','goods.market_id','=','market.id')
				->select('goods.*','cate.name','market.mar_name')
				->where('goods_name','like','%'.$request->input('keywords').'%')
				->orderBy('id','asc')
				->paginate($num);
		}else{
			//查询所有商品
			$goods = DB::table('goods')
				->join('cate','goods.cate_id','=','cate.id')
				->join('market','goods.market_id','=','market.id')
				->select('goods.*','cate.name','market.mar_name')
				->orderBy('id','asc')
				->paginate($num);
		}
		
		// dd($goods);
		//解析模板
		return view('admin.goods.index',['goods'=>$goods,'request'=>$request->all()]);
	}

	/*
		显示修改页面
	*/
	public function getEdit($id)
	{
		//查询当前商品的详细信息
		$goods = DB::table('goods')->where('id',$id)->first();
		$goods->goods_color = explode(',',$goods->goods_color);
		$goods->goods_size = explode(',',$goods->goods_size);

		//查询所有店铺
		$market = DB::table('market')->get();

		//查询所有分类
		$cates = DB::table('cate')->get();
		//查询所有三级分类
		$data = [];
		foreach($cates as $k=>$v){
			if(count(explode(',',$v->path)) == 3){
				$data[] = $v;
			}
		}

		return view('admin.goods.edit',['goods'=>$goods,'cates'=>$data,'market'=>$market]);
	}

	public function postUpdate(GoodsUpdateRequest $request)
	{
		//接收id
		$id = $request->input('id');
		//判断用户是是否上传图片
		if($request->hasFile('goods_pic')){
			//调用上传方法
			$upload = $this->upload($request,'goods_pic');
			//判断是否上传成功
			if($upload == 1){
				return back()->with('error','上传文件不合法');
			}
			//提取部分数据
			$data = $request->only(['cate_id','market_id','goods_price','goods_content','goods_name','rebate','goods_stock']);
			$data['goods_color'] = implode(',',$request->input('goods_color'));
			$data['goods_size'] = implode(',',$request->input('goods_size'));
			$data['goods_pic'] = $upload;
		}else{
			//提取部分数据
			$data = $request->only(['cate_id','market_id','goods_price','goods_content','goods_name','rebate','goods_stock']);
			$data['goods_color'] = implode(',',$request->input('goods_color'));
			$data['goods_size'] = implode(',',$request->input('goods_size'));
		}
		// dd($data);
		$res = DB::table('goods')->where('id',$id)->update($data);
		//判断
		if($res){
			return redirect('/admin/goods/index')->with('success','商品修改成功');
		}else{
			return back()->with('error','商品修改失败');
		}
	}

	/*
		删除商品
	*/
	public function postDelete(Request $request)
	{
		//接收id
		$id = $request->input('id');
		//执行删除
		$res = DB::table('goods')->where('id',$id)->delete();
		//判断
		if($res){
			echo 2;
		}else{
			echo 1;
		}
	}

	/*
		文件上传
	*/
	public function upload(Request $request,$filename)
	{	
		//随机文件名
		$name = md5(time()+rand(1,9999999999999));
		//获取后缀名
		$su = $request->file($filename)->getClientOriginalExtension();
		//定义数组 判断上传文件类型
		$arr = ['jpg','jpeg','png','gif','tiff'];
		//判断
		if(!in_array($su,$arr)){
			return 1;
		}
		//移动文件
		$request->file($filename)->move('./uploads/',$name.'.'.$su);

		//返回文件路径
		return '/uploads/'.$name.'.'.$su;
	}
   
   /*
		按照格式显示分类
   */
	static public function CateList()
	{
		//查询所有分类
		$cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
		foreach($cates as $k=>$v){
			//将路径转化为数组
			$arr = explode(',',$v->path);
			//计算数组的长度
			$len = count($arr)-1;
			$cates[$k]->name = str_repeat('|----',$len).$v->name;
		}

		return $cates;
	}

	// 商品订单
	public function getOrder(Request $request)
	{
		$num = $request->input('num',5);
        // $cates = self::CateList();
        if($request->input('keywords')){
        $data = DB::table('order')->join('goods','order.goods_id','=','goods.id')
		 		->select('order.*','goods.goods_name','goods.goods_pic')->where('goods.goods_name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{

		 $data = DB::table('order')->join('goods','order.goods_id','=','goods.id')
		 		->select('order.*','goods.goods_name','goods.goods_pic')->paginate($num);
        // dd($cates);
        }
		 // dd($data);
		return view('admin.order.index',['data'=>$data]);
	}

	// 发货设置
	public function getOsend(Request $request)
	{
		$id = $request->input("id");

		$res = DB::table('order')->where('id',$id)->update(['pay'=>3,'stime'=>date('Y-m-d H:i:s', time())]);

		// return redirect('/admin/goods/order');
		// dd($request->input('id'));
		// dd($id);
		if($res){
			return redirect('/admin/goods/order');
		}else{
			echo '确认失败,请联系管理员';
		}
	}
}  
